//  This is the HTTP subsystem script for the Helixoide SlServ Device
//  It implements the Helixoide SlServ protocol for Version 1.5
//  Current Implementation is based on irc.helixoide.com (DarkDna irc network)
//  If you want to help Develop contact Ross D. at posiden@helixoide.com

//  Helixoide Solarian Sl-Status system: httpHandler.lsl
//  Based On The Helixoide SlServ Device 1.5 beta 2
//  Created by Ross Delinger (aka: Posiden Dryke)
//  Last Update: Sunday 28th of Feburary 2010 11:39AM EST
//  SlServ Requirments: Helixoide SLServ 1.5
//  SlServ Code can be Found on http://code.google.com
string theUrl;
key requestURLKey;
//queue vars
list queue;
float time;
float last_time;
list explode(string data,list sep)
{
    return llParseString2List(data,sep,[]);
}
string implode(list data, string sep)
{
    return llDumpList2String(data,sep);
}

default
{
    state_entry()
    {
        
    }
    link_message(integer snum, integer num, string str, key id)
    {
        if((string)id=="init")
        {
        	if((integer)str == 1)
        	{
        		
            	requestURLKey = llRequestURL();
            }
            else
            {
            	list message = [(string)llGetKey(),"REGISTER",(string)llGetKey(),(string)llGetPos(),llGetRegionName(),theUrl,llGetObjectDesc()];
                string packedMsg = llDumpList2String(message,"&");
                llMessageLinked(LINK_THIS,1337,packedMsg,"MSG");
            }
        }
        if((string)id=="MSG")
        {
            llSetTimerEvent(0);
            last_time = time;
            time = llGetTime();
            if(time-last_time<=2.0 && last_time!=0)
            {
                llSetTimerEvent(0);
                queue += str;
                llSetTimerEvent(1.5);
            }
            if(llGetListLength(queue)==4)
            {
                llSetTimerEvent(0);
                string buffer = "queued<<" + implode(queue,":*:");
                queue = [];  
                llHTTPRequest("http://www.helixoide.com:8080/slserv/com/",[HTTP_METHOD,"POST"],buffer);    
                                          
                //replace the helixoide.com thing with the dynamic url getter thingy (config notecard?)
            }
            if(time-last_time>2.0 || last_time == 0.0)
            {
                llSetTimerEvent(0);
                if(llGetListLength(queue)!=0)
                {
                    if(llGetListLength(queue)==1)
                    {
                        string buffer = "non-queued<<" + llList2String(queue,0);
                        llHTTPRequest("http://www.helixoide.com:8080/slserv/com/",[HTTP_METHOD,"POST"],buffer);
                        
                    }
                    else
                    {
                        string buffer = "queued<<" + implode(queue,":*:");
                        llHTTPRequest("http://www.helixoide.com:8080/slserv/com/",[HTTP_METHOD,"POST"],buffer);
                        
                    }
                }
                if(llGetListLength(queue)==0)
                {
                    string buffer = "non-queued<<" + str;
                    llHTTPRequest("http://www.helixoide.com:8080/slserv/com/",[HTTP_METHOD,"POST"],buffer);
                    
                }
                queue = [];
                    
            }
                        
        }
            
            
    }
    timer()
    {
        llSetTimerEvent(0);
        if(llGetListLength(queue)!=0)
        {
            if(llGetListLength(queue)==1)
            {
                string buffer = "non-queued<<" + llList2String(queue,1);
                llHTTPRequest("http://www.helixoide.com:8080/slserv/com/",[HTTP_METHOD,"POST"],buffer);
                
            }
            else
            {
                string buffer = "queued<<" + implode(queue,":*:");
                llHTTPRequest("http://www.helixoide.com:8080/slserv/com/",[HTTP_METHOD,"POST"],buffer);
                
            }
            queue = [];
        }
    }
    http_response(key id, integer status, list meta, string body)
    {
        //all good, might do other stuff here later
    }
    http_request(key id, string method, string body)
    {
        if(id==requestURLKey)
        {
            if(method == URL_REQUEST_GRANTED)
            {
                
                theUrl = body;
                list message = [(string)llGetKey(),"REGISTER",(string)llGetKey(),(string)llGetPos(),llGetRegionName(),theUrl,llGetObjectDesc()];
                string packedMsg = llDumpList2String(message,"&");
                llMessageLinked(LINK_THIS,1337,packedMsg,"MSG");
            }
            else if(method == URL_REQUEST_DENIED)
            {
                llOwnerSay("Error: no url recieved");
            }
        
        }
        else
        {
            list step1 = llParseString2List(body,["<<"],[]);//step one makes 2 parts out of the incoming line
            if(llList2String(step1,0)=="queued")
            {
                list step2 = llParseString2List(llList2String(step1,1),[":*:"],[]);//get the queued messages
                integer i = 0;
                for(i = 0;i<llGetListLength(step2);i++)
                {
                    list step3 = llParseString2List(llList2String(step2,i),["&"],[]);//seperate message args
                    string command = llList2String(step3,0);
                    string user = llList2String(step3,1);
                    string msg = llList2String(step3,2);
                    if(command=="PRIVMSG")
                    {
                        llMessageLinked(LINK_SET,1337,msg + "|" + user,command);
                    }
                }
            }
            if(llList2String(step1,0)=="non-queued")
            {
                    list step3 = llParseString2List(llList2String(step1,1),["&"],[]);//seperate message args
                    string command = llList2String(step3,0);
                    string user = llList2String(step3,1);
                    string msg = llList2String(step3,2);
                    if(command=="PRIVMSG")
                    {
                        llMessageLinked(LINK_SET,1337,msg + "|" + user,command);
                    }
            }
            llHTTPResponse(id,200,"RETURN|success");
        }
    }
    changed(integer change)
    {
        if(change & CHANGED_REGION || change & CHANGED_TELEPORT || change & CHANGED_REGION_START)
        {
            llMessageLinked(LINK_THIS,1337,"1","init");
        }
    }
            
}